public class GoToLinePanel : DocPanelBase
{
	private Gtk.SpinButton line_number;
	private Gtk.Label line_count_lbl;	

	public GoToLinePanel (ref Gtk.SourceBuffer source_buff, ref Gtk.SourceView source_view)
	{
		base (1, 5, false, ref source_buff, ref source_view);
				
		this.attach (
			new Gtk.Image.from_stock (Gtk.STOCK_JUMP_TO, Gtk.IconSize.MENU),
			0, 1, 0, 1, Gtk.AttachOptions.SHRINK, Gtk.AttachOptions.SHRINK, 5, 0 
		);
		
		Gtk.Label desc = new Gtk.Label ("<b>Przejdź do lini: </b>");
		desc.set_use_markup (true);		
		this.attach (
			desc, 1, 2, 0, 1, Gtk.AttachOptions.SHRINK, Gtk.AttachOptions.SHRINK, 5, 0
		);
		
		this.line_number = new Gtk.SpinButton.with_range (
			1, 1, 1
		);
		this.line_number.set_size_request (150, -1);	
		this.line_number.key_press_event += this.on_goto_line;
		this.attach_defaults (this.line_number, 2, 3, 0, 1);		
		
		this.line_count_lbl = new Gtk.Label ("");
		this.line_count_lbl.set_use_markup (true);
		this.attach (this.line_count_lbl, 3, 4, 0, 1, Gtk.AttachOptions.SHRINK, Gtk.AttachOptions.SHRINK, 5, 0);
		
		this.show_panel += this.on_show_panel;
	}
	
	protected virtual bool on_show_panel ()
	{
		this.source_view.set_editable (false);

		this.line_number.set_range (0, this.source_buff.get_line_count ());
		this.line_number.grab_focus ();
		
		this.line_count_lbl.set_markup (" z <b>" + this.source_buff.get_line_count().to_string () + "</b>");
		return false;
	}
	
	protected bool on_goto_line (Gtk.SpinButton sender, Gdk.EventKey event)
	{
		this.line_number.update ();
		if (Gdk.keyval_name(event.keyval) == "Return")
		{
			Gtk.TextIter line_iter;
			this.source_buff.get_iter_at_line (out line_iter, this.line_number.get_value_as_int ());
			this.source_buff.place_cursor (line_iter);
			
			Gtk.TextMark mark = this.source_buff.create_mark ("cursor", line_iter, true);
        		this.source_view.scroll_to_mark (mark, 0.4, false, 0, 0);
        		this.source_buff.delete_mark (mark);
			
			this.source_view.set_editable (true);
			this.source_view.grab_focus ();
			this.close ();
		}
		return false;
	}
}
